package com.example.demo2.dao.impl;



import com.example.demo2.dao.UserDAO;
import com.example.demo2.model.User;
import com.example.demo2.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDAO {
    @Override
    public User findByUserNameAndPassword(String name, String pass) {
        String sql = "select * from user where username = ? and password = ?";
        User user = new User();
        DBUtil dbUtil = new DBUtil();
        try(connection = dbUtil.getConnection()
                PreparedStatement prep = conn.prepareStatement(sql);
        ){
            prep.setString(1,name);
            prep.setString(2,pass);
            try(){

            }
            //数据查询
            prep.setString(1,name);
            prep.setString(2,pass);
            try(ResultSet rs = prep.executeQuery()){
                try(
                        Connection conn = dbUtil.getConnection();
                        PreparedStatement prep = conn.prepareStatement(sql);
                ){
                    //数据查询
                    prep.setString(1,name);
                    prep.setString(2,pass);
                    try(ResultSet rs = prep.executeQuery();){
                        //数据结果集
                        while (rs.next()) {
                            user.setId(rs.getInt("id"));
                            user.setUsername(rs.getString("username"));
                            user.setPassword(rs.getString("password"));
                            user.setName(rs.getString("name"));
                            user.setRole(rs.getString("role"));
                            user.setEmail(rs.getString("email"));
                            user.setPhone(rs.getString("phone"));
                            user.setCreate_time(
                                    rs.getTimestamp("create_time")
                                            .toLocalDateTime());
                            user.setStatus(rs.getInt("status"));
                        }
                        return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean userSignUp() {
        return false;
    }

    @Override
    public User findByUsername() {
        return null;
    }
}
